###############################################################################
## AUTHOR: ALAN YAN
## DATE LAST UPDATED: 07/18/2020
## PURPOSE: CLEAN YOUGOV CONJOINT DATA
###############################################################################
rm(list = ls())

#### PACKAGES ####
library(pacman)
p_load(tidyverse,
       estimatr,
       broom,
       haven,
       psych,
       vroom)

#### LOAD DATA ####
df <- vroom("01-yougov-conjoint/01-data/raw/yougov_raw.csv")

#### CODE DATA ####
#code covariates 
df %>%
  mutate(
    rid = c(1:nrow(df)),
    r_state = as.factor(inputstate),
    r_age = 2020 - birthyr,
    r_female = as.factor(ifelse(gender == 2, "Female", "Male")),
    r_edu = as.factor(
      case_when(
        educ == 1 ~ "No HS",
        educ == 2 ~ "High school graduate",
        educ == 3 ~ "Some college",
        educ == 4 ~ "2-year college",
        educ == 5 ~ "4-year college",
        educ == 6 ~ "Post-grad"
      )
    ),
    r_race = as.factor(
      case_when(
        race == 1 ~ "White",
        race == 2 ~ "Black",
        race == 3 ~ "Hispanic",
        race == 4 ~ "Asian",
        race == 5 ~ "Native American",
        race == 6 ~ "Mixed",
        race == 7 ~ "Other",
        race == 8 ~ "Middle Eastern"
      )
    ),
    r_hispanic = as.factor(ifelse(hispanic == "Yes", "Hispanic", "Not Hispanic")),
    r_employment = as.factor(
      case_when(
        employ == 1 ~ "Full-time",
        employ == 2 ~ "Part-time",
        employ == 3 ~ "Temporarily laid off",
        employ == 4 ~ "Unemployed",
        employ == 5 ~ "Retired",
        employ == 6 ~ "Permanently disabled",
        employ == 7 ~ "Homemaker",
        employ == 8 ~ "Student",
        employ == 9 ~ "Other"
      )
    ),
    r_union_member = ifelse(union_combined_1 == 1, "Union member", "Not a union member"),
    r_industry = as.factor(
      case_when(
        main_industry == 1 ~ "Construction",
        main_industry == 2 ~ "Education",
        main_industry == 3 ~ "Financial activities, insurance, or real estate",
        main_industry == 4 ~ "Health care",
        main_industry == 5 ~ "Information technology",
        main_industry == 6 ~ "Leisure and hospitality, including arts, entertainment, and recreation",
        main_industry == 7 ~ "Manufacturing",
        main_industry == 8 ~ "Natural resources and mining (such as agriculture and forestry)",
        main_industry == 9 ~ "Professional, scientific, technical services, and management",
        main_industry == 10 ~ "Public administration, including federal, state, and local government",
        main_industry == 11 ~ "Transportation and warehouses",
        main_industry == 12 ~ "Utilities",
        main_industry == 13 ~ "Wholesale or retail",
        main_industry == 14 ~ "Other industry",
        main_industry == 15 ~ "Don’t know"
      )
    ),
    r_industry.blue_white = case_when(
      r_industry %in% c("Construction",
                        "Leisure and hospitality, including arts, entertainment, and recreation",
                        "Manufacturing",
                        "Natural resources and mining (such as agriculture and forestry)",
                        "Transportation and warehouses",
                        "Utilities", 
                        "Wholesale or retail") ~ "Blue-collar",
      r_industry %in% c("Education",
                        "Financial activities, insurance, or real estate",
                        "Health care",
                        "Information technology",
                        "Professional, scientific, technical services, and management",
                        "Public administration, including federal, state, and local government") ~ "White-collar"
    ),
    r_income = case_when(
      faminc_new %in% c(1:16) ~ faminc_new
    ),
    r_income = (r_income - mean(r_income, na.rm = TRUE))/sd(r_income, na.rm = TRUE),
    r_education_continuous = (educ - mean(educ, na.rm = TRUE))/sd(educ, na.rm = TRUE),
    r_party = ifelse(pid7 == 8, NA, pid7),
    r_ideology = ifelse(ideo5 == 6, NA, ideo5)
  ) -> df

#filling in missing data for union 
df$r_union_member <- ifelse(is.na(df$r_union_member) == TRUE, "Not a union member", df$r_union_member)

#create firm specific dataframes
firm1 <- df %>%
  mutate(
    work_binary = ifelse(q126_1 == 1, 1, 0),
    complaints_binary = ifelse(q130_1 == 1, 1, 0),
    responsibility_binary = ifelse(q132_1 == 1, 1, 0),
    power_binary = ifelse(q133_1 == 1, 1, 0),
    cje_corporate_gov = cje_cgs_01_a,
    cje_corporate_resp = cje_csr_01_a,
    cje_firm_size = cje_fs_01_a,
    cje_political_donations = cje_fpc_01_a,
    cje_gender_ownership = cje_gdp_01_a,
    cje_healthcare = cje_hic_01_a,
    cje_hours = cje_how_01_a,
    cje_training = cje_jtp_01_a,
    cje_location = cje_l_01_a,
    cje_sick_leave = cje_psl_01_a,
    cje_parental_leave = cje_pl_01_a,
    cje_race_ownership = cje_rdp_01_a,
    cje_retirement = cje_rm_01_a,
    cje_income = cje_s_01_a,
    cje_type_of_work = cje_tot_01_a,
    cje_union = cje_u_01_a,
    cje_work_from_home = cje_wfh_01_a,
    cje_team_work = cje_wwo_01_a,
    cje_work_culture = cje_wc_01_a,
    cje_trial = 1
  )

firm2 <- df %>%
  mutate(
    work_binary = ifelse(q126_1 == 2, 1, 0),
    complaints_binary = ifelse(q130_1 == 2, 1, 0),
    responsibility_binary = ifelse(q132_1 == 2, 1, 0),
    power_binary = ifelse(q133_1 == 2, 1, 0),
    cje_corporate_gov = cje_cgs_01_b,
    cje_corporate_resp = cje_csr_01_b,
    cje_firm_size = cje_fs_01_b,
    cje_political_donations = cje_fpc_01_b,
    cje_gender_ownership = cje_gdp_01_b,
    cje_healthcare = cje_hic_01_b,
    cje_hours = cje_how_01_b,
    cje_training = cje_jtp_01_b,
    cje_location = cje_l_01_b,
    cje_sick_leave = cje_psl_01_b,
    cje_parental_leave = cje_pl_01_b,
    cje_race_ownership = cje_rdp_01_b,
    cje_retirement = cje_rm_01_b,
    cje_income = cje_s_01_b,
    cje_type_of_work = cje_tot_01_b,
    cje_union = cje_u_01_b,
    cje_work_from_home = cje_wfh_01_b,
    cje_team_work = cje_wwo_01_b,
    cje_work_culture = cje_wc_01_b,
    cje_trial = 1
  ) 

firm3 <- df %>%
  mutate(
    work_binary = ifelse(q126_2 == 1, 1, 0),
    complaints_binary = ifelse(q130_2 == 1, 1, 0),
    responsibility_binary = ifelse(q132_2 == 1, 1, 0),
    power_binary = ifelse(q133_2 == 1, 1, 0),
    cje_corporate_gov = cje_cgs_02_a,
    cje_corporate_resp = cje_csr_02_a,
    cje_firm_size = cje_fs_02_a,
    cje_political_donations = cje_fpc_02_a,
    cje_gender_ownership = cje_gdp_02_a,
    cje_healthcare = cje_hic_02_a,
    cje_hours = cje_how_02_a,
    cje_training = cje_jtp_02_a,
    cje_location = cje_l_02_a,
    cje_sick_leave = cje_psl_02_a,
    cje_parental_leave = cje_pl_02_a,
    cje_race_ownership = cje_rdp_02_a,
    cje_retirement = cje_rm_02_a,
    cje_income = cje_s_02_a,
    cje_type_of_work = cje_tot_02_a,
    cje_union = cje_u_02_a,
    cje_work_from_home = cje_wfh_02_a,
    cje_team_work = cje_wwo_02_a,
    cje_work_culture = cje_wc_02_a,
    cje_trial = 2
  ) 

firm4 <- df %>%
  mutate(
    work_binary = ifelse(q126_2 == 2, 1, 0),
    complaints_binary = ifelse(q130_2 == 2, 1, 0),
    responsibility_binary = ifelse(q132_2 == 2, 1, 0),
    power_binary = ifelse(q133_2 == 2, 1, 0),
    cje_corporate_gov = cje_cgs_02_b,
    cje_corporate_resp = cje_csr_02_b,
    cje_firm_size = cje_fs_02_b,
    cje_political_donations = cje_fpc_02_b,
    cje_gender_ownership = cje_gdp_02_b,
    cje_healthcare = cje_hic_02_b,
    cje_hours = cje_how_02_b,
    cje_training = cje_jtp_02_b,
    cje_location = cje_l_02_b,
    cje_sick_leave = cje_psl_02_b,
    cje_parental_leave = cje_pl_02_b,
    cje_race_ownership = cje_rdp_02_b,
    cje_retirement = cje_rm_02_b,
    cje_income = cje_s_02_b,
    cje_type_of_work = cje_tot_02_b,
    cje_union = cje_u_02_b,
    cje_work_from_home = cje_wfh_02_b,
    cje_team_work = cje_wwo_02_b,
    cje_work_culture = cje_wc_02_b,
    cje_trial = 2
  ) 

firm5 <- df %>%
  mutate(
    work_binary = ifelse(q126_3 == 1, 1, 0),
    complaints_binary = ifelse(q130_3 == 1, 1, 0),
    responsibility_binary = ifelse(q132_3 == 1, 1, 0),
    power_binary = ifelse(q133_3 == 1, 1, 0),
    cje_corporate_gov = cje_cgs_03_a,
    cje_corporate_resp = cje_csr_03_a,
    cje_firm_size = cje_fs_03_a,
    cje_political_donations = cje_fpc_03_a,
    cje_gender_ownership = cje_gdp_03_a,
    cje_healthcare = cje_hic_03_a,
    cje_hours = cje_how_03_a,
    cje_training = cje_jtp_03_a,
    cje_location = cje_l_03_a,
    cje_sick_leave = cje_psl_03_a,
    cje_parental_leave = cje_pl_03_a,
    cje_race_ownership = cje_rdp_03_a,
    cje_retirement = cje_rm_03_a,
    cje_income = cje_s_03_a,
    cje_type_of_work = cje_tot_03_a,
    cje_union = cje_u_03_a,
    cje_work_from_home = cje_wfh_03_a,
    cje_team_work = cje_wwo_03_a,
    cje_work_culture = cje_wc_03_a,
    cje_trial = 3
  ) 

firm6 <- df %>%
  mutate(
    work_binary = ifelse(q126_3 == 2, 1, 0),
    complaints_binary = ifelse(q130_3 == 2, 1, 0),
    responsibility_binary = ifelse(q132_3 == 2, 1, 0),
    power_binary = ifelse(q133_3 == 2, 1, 0),
    cje_corporate_gov = cje_cgs_03_b,
    cje_corporate_resp = cje_csr_03_b,
    cje_firm_size = cje_fs_03_b,
    cje_political_donations = cje_fpc_03_b,
    cje_gender_ownership = cje_gdp_03_b,
    cje_healthcare = cje_hic_03_b,
    cje_hours = cje_how_03_b,
    cje_training = cje_jtp_03_b,
    cje_location = cje_l_03_b,
    cje_sick_leave = cje_psl_03_b,
    cje_parental_leave = cje_pl_03_b,
    cje_race_ownership = cje_rdp_03_b,
    cje_retirement = cje_rm_03_b,
    cje_income = cje_s_03_b,
    cje_type_of_work = cje_tot_03_b,
    cje_union = cje_u_03_b,
    cje_work_from_home = cje_wfh_03_b,
    cje_team_work = cje_wwo_03_b,
    cje_work_culture = cje_wc_03_b,
    cje_trial = 3
  ) 

firm7 <- df %>%
  mutate(
    work_binary = ifelse(q126_4 == 1, 1, 0),
    complaints_binary = ifelse(q130_4 == 1, 1, 0),
    responsibility_binary = ifelse(q132_4 == 1, 1, 0),
    power_binary = ifelse(q133_5 == 1, 1, 0),
    cje_corporate_gov = cje_cgs_04_a,
    cje_corporate_resp = cje_csr_04_a,
    cje_firm_size = cje_fs_04_a,
    cje_political_donations = cje_fpc_04_a,
    cje_gender_ownership = cje_gdp_04_a,
    cje_healthcare = cje_hic_04_a,
    cje_hours = cje_how_04_a,
    cje_training = cje_jtp_04_a,
    cje_location = cje_l_04_a,
    cje_sick_leave = cje_psl_04_a,
    cje_parental_leave = cje_pl_04_a,
    cje_race_ownership = cje_rdp_04_a,
    cje_retirement = cje_rm_04_a,
    cje_income = cje_s_04_a,
    cje_type_of_work = cje_tot_04_a,
    cje_union = cje_u_04_a,
    cje_work_from_home = cje_wfh_04_a,
    cje_team_work = cje_wwo_04_a,
    cje_work_culture = cje_wc_04_a,
    cje_trial = 4
  ) 

firm8 <- df %>%
  mutate(
    work_binary = ifelse(q126_4 == 2, 1, 0),
    complaints_binary = ifelse(q130_4 == 2, 1, 0),
    responsibility_binary = ifelse(q132_4 == 2, 1, 0),
    power_binary = ifelse(q133_5 == 2, 1, 0),
    cje_corporate_gov = cje_cgs_04_b,
    cje_corporate_resp = cje_csr_04_b,
    cje_firm_size = cje_fs_04_b,
    cje_political_donations = cje_fpc_04_b,
    cje_gender_ownership = cje_gdp_04_b,
    cje_healthcare = cje_hic_04_b,
    cje_hours = cje_how_04_b,
    cje_training = cje_jtp_04_b,
    cje_location = cje_l_04_b,
    cje_sick_leave = cje_psl_04_b,
    cje_parental_leave = cje_pl_04_b,
    cje_race_ownership = cje_rdp_04_b,
    cje_retirement = cje_rm_04_b,
    cje_income = cje_s_04_b,
    cje_type_of_work = cje_tot_04_b,
    cje_union = cje_u_04_b,
    cje_work_from_home = cje_wfh_04_b,
    cje_team_work = cje_wwo_04_b,
    cje_work_culture = cje_wc_04_b,
    cje_trial = 4
  ) 

#check length of each firm dataframe
nrow(firm1)
nrow(firm2)
nrow(firm3)
nrow(firm4)
nrow(firm5)
nrow(firm6)
nrow(firm7)
nrow(firm8)

#keep columns
cje_vars <- c("rid", "weight", "work_binary", "complaints_binary", "responsibility_binary", "power_binary", 
              "cje_corporate_gov", "cje_corporate_resp", "cje_firm_size", "cje_political_donations", "cje_gender_ownership",
              "cje_healthcare", "cje_hours", "cje_training", "cje_location", "cje_sick_leave", "cje_parental_leave", 
              "cje_race_ownership", "cje_retirement", "cje_income", "cje_type_of_work", "cje_union", "cje_work_from_home",
              "cje_team_work", "cje_work_culture", "cje_trial")

covariates <- c("r_state", "r_age", "r_female", "r_edu", "r_race", "r_hispanic", "r_employment",
                "r_union_member", "r_industry", "r_party", "r_ideology", "r_industry.blue_white", 
                "r_income", "r_education_continuous")

#binding them together
df_cleaned <- rbind(
  firm1[c(cje_vars, covariates)],
  firm2[c(cje_vars, covariates)],
  firm3[c(cje_vars, covariates)],
  firm4[c(cje_vars, covariates)],
  firm5[c(cje_vars, covariates)],
  firm6[c(cje_vars, covariates)],
  firm7[c(cje_vars, covariates)],
  firm8[c(cje_vars, covariates)]
)

#recoding the treatments to character
df_cleaned %>%
  mutate(
    cje_corporate_gov = 
      case_when(
        cje_corporate_gov == 1 ~ "Workers sit on the corporate board",
        cje_corporate_gov == 2 ~ "Workers elect their managers",
        cje_corporate_gov == 3 ~ "Workers are shareholders",
        cje_corporate_gov == 4 ~ "Privately owned by non-worker shareholders",
        cje_corporate_gov == 5 ~ "Publicly owned by shareholders",
        cje_corporate_gov == 6 ~ "Privately owned by one individual"
      ),
    cje_corporate_resp = 
      case_when(
        cje_corporate_resp == 1 ~ "No corporate social responsibility",
        cje_corporate_resp == 2 ~ "Commitment to corporate social responsibility"
      ),
    cje_firm_size = 
      case_when(
        cje_firm_size == 1 ~ "50 co-workers",
        cje_firm_size == 2 ~ "250 co-workers",
        cje_firm_size == 3 ~ "500 co-workers",
        cje_firm_size == 4 ~ "1,000 co-workers",
        cje_firm_size == 5 ~ "5,000 co-workers"
      ),
    cje_political_donations = 
      case_when(
        cje_political_donations == 1 ~ "Primarily donates to Democrats",
        cje_political_donations == 2 ~ "Donates to both Democrats and Republicans",
        cje_political_donations == 3 ~ "Primarily donates to Republicans"
      ),
    cje_gender_ownership = 
      case_when(
        cje_gender_ownership == 1 ~ "Women owned",
        cje_gender_ownership == 2 ~ "Majority-women owned",
        cje_gender_ownership == 3 ~ "Majority-men owned",
        cje_gender_ownership == 4 ~ "Men owned"
      ),
    cje_healthcare = 
      case_when(
        cje_healthcare == 1 ~ "Employer pays 50%",
        cje_healthcare == 2 ~ "Employer pays 75%",
        cje_healthcare == 3 ~ "Employer pays 100%"
      ),
    cje_hours = 
      case_when(
        cje_hours == 1 ~ "40 hours",
        cje_hours == 2 ~ "50 hours",
        cje_hours == 3 ~ "60 hours",
        cje_hours == 4 ~ "70 hours",
        cje_hours == 5 ~ "80 hours"
      ),
    cje_training = 
      case_when(
        cje_training == 1 ~ "Veterans training",
        cje_training == 2 ~ "Mentally-disabled training",
        cje_training == 3 ~ "Ex-felons training",
        cje_training == 4 ~ "No special job training program"
      ),
    cje_location = 
      case_when(
        cje_location == 1 ~ "Located in your city",
        cje_location == 2 ~ "Located in a different city",
        cje_location == 3 ~ "Located in a different city but pays for relocation"
      ),
    cje_sick_leave = 
      case_when(
        cje_sick_leave == 1 ~ "No paid sick leave",
        cje_sick_leave == 2 ~ "Two weeks paid sick leave days",
        cje_sick_leave == 3 ~ "Unlimited paid sick leave days"
      ),
    cje_parental_leave = 
      case_when(
        cje_parental_leave == 1 ~ "No parental leave policy",
        cje_parental_leave == 2 ~ "Generous parental leave policy"
      ),
    cje_race_ownership = 
      case_when(
        cje_race_ownership == 1 ~ "All people of color owned",
        cje_race_ownership == 2 ~ "Majority people of color owned",
        cje_race_ownership == 3 ~ "Majority white owned",
        cje_race_ownership == 4 ~ "All white owned"
      ),
    cje_retirement = 
      case_when(
        cje_retirement == 1 ~ "Matches 100% of 401k contributions",
        cje_retirement == 2 ~ "Matches 75% of 401k contributions",
        cje_retirement == 3 ~ "Matches 50% of 401k contributions",
        cje_retirement == 4 ~ "Matches 25% of 401k contributions",
        cje_retirement == 5 ~ "Matches 0% of 401k contributions"
      ),
    cje_income = 
      case_when(
        cje_income == 1 ~ "$30,000 per year",
        cje_income == 2 ~ "$40,000 per year",
        cje_income == 3 ~ "$50,000 per year",
        cje_income == 4 ~ "$60,000 per year",
        cje_income == 5 ~ "$70,000 per year",
        cje_income == 6 ~ "$80,000 per year",
        cje_income == 7 ~ "$90,000 per year",
        cje_income == 8 ~ "$100,000 per year",
        cje_income == 9 ~ "$110,000 per year",
        cje_income == 10 ~ "$120,000 per year",
        cje_income == 11 ~ "$130,000 per year",
        cje_income == 12 ~ "$140,000 per year",
        cje_income == 13 ~ "$150,000 per year",
        cje_income == 14 ~ "$160,000 per year",
        cje_income == 15 ~ "$170,000 per year",
        cje_income == 16 ~ "$180,000 per year",
        cje_income == 17 ~ "$190,000 per year",
        cje_income == 18 ~ "$200,000 per year",
        cje_income == 19 ~ "$210,000 per year",
        cje_income == 20 ~ "$220,000 per year",
        cje_income == 21 ~ "$230,000 per year",
        cje_income == 22 ~ "$240,000 per year",
        cje_income == 23 ~ "$250,000 per year",
        cje_income == 24 ~ "$260,000 per year",
        cje_income == 25 ~ "$270,000 per year",
        cje_income == 26 ~ "$280,000 per year",
        cje_income == 27 ~ "$290,000 per year",
        cje_income == 28 ~ "$300,000 per year"
      ),
    cje_type_of_work = 
      case_when(
        cje_type_of_work == 1 ~ "Largely repetitive work",
        cje_type_of_work == 2 ~ "Mix of repetitive and creative work",
        cje_type_of_work == 3 ~ "Largely creative work"
      ),
    cje_union = 
      case_when(
        cje_union == 1 ~ "Unionized",
        cje_union == 2 ~ "Not unionized"
      ),
    cje_work_from_home = 
      case_when(
        cje_work_from_home == 1 ~ "Primarily works from home",
        cje_work_from_home == 2 ~ "Sometimes work from home",
        cje_work_from_home == 3 ~ "Cannot work from home"
      ),
    cje_team_work = 
      case_when(
        cje_team_work == 1 ~ "Mostly working alone",
        cje_team_work == 2 ~ "Mix of individual and team work",
        cje_team_work == 3 ~ "Mostly team work"
      ),
    cje_work_culture = 
      case_when(
        cje_work_culture == 1 ~ "Generally friendly and supportive",
        cje_work_culture == 2 ~ "Somewhat friendly and supportive",
        cje_work_culture == 3 ~ "Somewhat unfriendly and unsupportive",
        cje_work_culture == 4 ~ "Generally unfriendly and unsupportive"
      )
  ) -> df_cleaned

#recoding miss dependent variables as 0s
df_cleaned$work_binary <- ifelse(is.na(df_cleaned$work_binary) == TRUE, 0, df_cleaned$work_binary)
df_cleaned$complaints_binary <- ifelse(is.na(df_cleaned$complaints_binary) == TRUE, 0, df_cleaned$complaints_binary)
df_cleaned$responsibility_binary <- ifelse(is.na(df_cleaned$responsibility_binary) == TRUE, 0, df_cleaned$responsibility_binary)
df_cleaned$power_binary <- ifelse(is.na(df_cleaned$power_binary) == TRUE, 0, df_cleaned$power_binary)

#### RELEVEL FACTORS ####
#conjoint factors
#df_cleaned <- within(df_cleaned, cje_firm_size <- relevel(cje_firm_size, ref = "50 co-workers"))
#df_cleaned <- within(df_cleaned, cje_sick_leave <- relevel(cje_sick_leave, ref = "No paid sick leave"))
#df_cleaned <- within(df_cleaned, cje_healthcare <- relevel(cje_healthcare, ref = "Employer pays 50%"))
#df_cleaned <- within(df_cleaned, cje_hours <- relevel(cje_hours, ref = "40 hours"))
#df_cleaned <- within(df_cleaned, cje_corporate_gov <- relevel(cje_corporate_gov, ref = "Privately owned by non-worker shareholders"))
#df_cleaned <- within(df_cleaned, cje_race_ownership <- relevel(cje_race_ownership, ref = "Majority white owned"))
#df_cleaned <- within(df_cleaned, cje_gender_ownership <- relevel(cje_gender_ownership, ref = "Majority-men owned"))
#df_cleaned <- within(df_cleaned, cje_corporate_resp <- relevel(cje_corporate_resp, ref = "No corporate social responsibility"))
#df_cleaned <- within(df_cleaned, cje_parental_leave <- relevel(cje_parental_leave, ref = "No parental leave policy"))
#df_cleaned <- within(df_cleaned, cje_type_of_work <- relevel(cje_type_of_work, ref = "Mix of repetitive and creative work"))
#df_cleaned <- within(df_cleaned, cje_team_work <- relevel(cje_team_work, ref = "Mix of individual and team work"))
#df_cleaned <- within(df_cleaned, cje_retirement <- relevel(cje_retirement, ref = "Matches 0% of 401k contributions"))
#df_cleaned <- within(df_cleaned, cje_location <- relevel(cje_location, ref = "Located in your city"))
#df_cleaned <- within(df_cleaned, cje_union <- relevel(cje_union, ref = "Not unionized"))
#df_cleaned <- within(df_cleaned, cje_work_culture <- relevel(cje_work_culture, ref = "Generally unfriendly and unsupportive"))
#df_cleaned <- within(df_cleaned, cje_training <- relevel(cje_training, ref = "No special job training program"))
#df_cleaned <- within(df_cleaned, cje_work_from_home <- relevel(cje_work_from_home, ref = "Cannot work from home"))
#df_cleaned <- within(df_cleaned, cje_political_donations <- relevel(cje_political_donations, ref = "Donates to both Democrats and Republicans"))
#df_cleaned <- within(df_cleaned, cje_income <- relevel(cje_income, ref = "$30,000 per year"))

#covariate factors
df_cleaned <- within(df_cleaned, r_edu <- relevel(r_edu, ref = "No HS"))
df_cleaned <- within(df_cleaned, r_race <- relevel(r_race, ref = "White"))
df_cleaned <- within(df_cleaned, r_hispanic <- relevel(r_hispanic, ref = "Not Hispanic"))
df_cleaned <- within(df_cleaned, r_employment <- relevel(r_employment, ref = "Unemployed"))
#df_cleaned <- within(df_cleaned, r_union_member <- relevel(r_union_member, ref = "Not a union member"))

#relevel factors with a natural ordering
df_cleaned %>%
  mutate(cje_income = fct_relevel(cje_income,
                                  "$30,000 per year",
                                  "$40,000 per year",
                                  "$50,000 per year",
                                  "$60,000 per year",
                                  "$70,000 per year",
                                  "$80,000 per year",
                                  "$90,000 per year",
                                  "$100,000 per year",
                                  "$110,000 per year",
                                  "$120,000 per year",
                                  "$130,000 per year",
                                  "$140,000 per year",
                                  "$150,000 per year",
                                  "$160,000 per year",
                                  "$170,000 per year",
                                  "$180,000 per year",
                                  "$190,000 per year",
                                  "$200,000 per year",
                                  "$210,000 per year",
                                  "$220,000 per year",
                                  "$230,000 per year",
                                  "$240,000 per year",
                                  "$250,000 per year",
                                  "$260,000 per year",
                                  "$270,000 per year",
                                  "$280,000 per year",
                                  "$290,000 per year",
                                  "$300,000 per year"),
         cje_firm_size = fct_relevel(cje_firm_size,
                                     "50 co-workers",
                                     "250 co-workers",
                                     "500 co-workers",
                                     "1,000 co-workers",
                                     "5,000 co-workers"),
         cje_sick_leave = fct_relevel(cje_sick_leave,
                                      "No paid sick leave",
                                      "Two weeks paid sick leave days",
                                      "Unlimited paid sick leave days"),
         cje_gender_ownership = fct_relevel(cje_gender_ownership,
                                            "Men owned",
                                            "Majority-men owned",
                                            "Majority-women owned",
                                            "Women owned"),
         cje_healthcare = fct_relevel(cje_healthcare,
                                      "Employer pays 50%",
                                      "Employer pays 75%",
                                      "Employer pays 100%"),
         cje_race_ownership = fct_relevel(cje_race_ownership,
                                          "All white owned",
                                          "Majority white owned",
                                          "Majority people of color owned",
                                          "All people of color owned"),
         cje_retirement = fct_relevel(cje_retirement,
                                      "Matches 0% of 401k contributions",
                                      "Matches 25% of 401k contributions",
                                      "Matches 50% of 401k contributions",
                                      "Matches 75% of 401k contributions",
                                      "Matches 100% of 401k contributions"),
         cje_hours = fct_relevel(cje_hours,
                                 "40 hours",
                                 "50 hours",
                                 "60 hours",
                                 "70 hours",
                                 "80 hours"),
         cje_corporate_gov = fct_relevel(cje_corporate_gov,
                                         "Privately owned by non-worker shareholders",
                                         "Privately owned by one individual",
                                         "Publicly owned by shareholders",
                                         "Workers are shareholders",
                                         "Workers sit on the corporate board",
                                         "Workers elect their managers"),
         cje_corporate_resp = fct_relevel(cje_corporate_resp,
                                          "No corporate social responsibility",
                                          "Commitment to corporate social responsibility"),
         cje_parental_leave = fct_relevel(cje_parental_leave,
                                          "No parental leave policy",
                                          "Generous parental leave policy"),
         cje_type_of_work = fct_relevel(cje_type_of_work,
                                        "Largely repetitive work",
                                        "Mix of repetitive and creative work",
                                        "Largely creative work"),
         cje_team_work = fct_relevel(cje_team_work,
                                     "Mostly working alone",
                                     "Mix of individual and team work",
                                     "Mostly team work"),
         cje_location = fct_relevel(cje_location,
                                    "Located in your city",
                                    "Located in a different city",
                                    "Located in a different city but pays for relocation"),
         cje_union = fct_relevel(cje_union,
                                 "Not unionized",
                                 "Unionized"),
         cje_work_culture = fct_relevel(cje_work_culture,
                                        "Generally unfriendly and unsupportive",
                                        "Somewhat unfriendly and unsupportive",
                                        "Somewhat friendly and supportive",
                                        "Generally friendly and supportive"),
         cje_training = fct_relevel(cje_training,
                                    "Ex-felons training",
                                    "Veterans training",
                                    "Mentally-disabled training"),
         cje_work_from_home = fct_relevel(cje_work_from_home,
                                          "Cannot work from home",
                                          "Sometimes work from home",
                                          "Primarily works from home"),
         cje_political_donations = fct_relevel(cje_political_donations,
                                               "Donates to both Democrats and Republicans",
                                               "Primarily donates to Democrats",
                                               "Primarily donates to Republicans"),
         r_union_member = fct_relevel(r_union_member,
                                      "Not a union member",
                                      "Union member")) -> df_cleaned

#factorize the respondent id
df_cleaned$rid <- as.factor(df_cleaned$rid)

#exporting data
write_rds(df_cleaned, "replication-folder/01-yougov-conjoint/01-data/cleaned/yougov2-conjoint-cleaned.rds")
